using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;

namespace SpiderService
{
	public class SpiderLogs
	{
		private static Dictionary<int, Dictionary<int, int>> s_ProjectLogs;

		static SpiderLogs()
		{
			s_ProjectLogs = new Dictionary<int, Dictionary<int, int>>();
		}

		public static void AddLogDB(int projectId, string url, int hashcode,int fileId, int status)
		{
			try
			{
				LoadLog(projectId);

				Dictionary<int, int> logs;
				lock ((s_ProjectLogs as ICollection).SyncRoot)
				{
					s_ProjectLogs.TryGetValue(projectId, out logs);
					if (logs == null)
					{
						logs = new Dictionary<int, int>();
						s_ProjectLogs.Add(projectId, logs);
					}
				}

				lock ((logs as ICollection).SyncRoot)
				{
					int oldStatus;
					logs.TryGetValue(hashcode, out oldStatus);
					if (oldStatus == 0)
					{
						com.spider.SpiderService spider = new com.spider.SpiderService();
						spider.AddSpiderLog(projectId, url, hashcode, fileId, status);

						logs[hashcode] = 1;
					}
				}
			}
			catch (ArgumentException ex)
			{
				Globals.FileLog.WriteError(string.Format("url:{0}, hashcode:{1}", url, hashcode));
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public static void AddLogToCache(int projectId, int hashcode)
		{
			try
			{
				LoadLog(projectId);

				Dictionary<int, int> logs;
				lock ((s_ProjectLogs as ICollection).SyncRoot)
				{
					s_ProjectLogs.TryGetValue(projectId, out logs);
					if (logs == null)
					{
						logs = new Dictionary<int, int>();
						s_ProjectLogs.Add(projectId, logs);
					}
				}

				lock ((logs as ICollection).SyncRoot)
				{
					logs.Add(hashcode, 0);
				}
			}
			catch (ArgumentException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public static void RemoveLog(int projectId, int hashcode)
		{
			try
			{
				LoadLog(projectId);

				Dictionary<int, int> logs;
				lock ((s_ProjectLogs as ICollection).SyncRoot)
				{
					s_ProjectLogs.TryGetValue(projectId, out logs);
				}

				if (logs != null)
				{
					lock ((logs as ICollection).SyncRoot)
					{
						logs.Remove(hashcode);
						com.spider.SpiderService spider = new com.spider.SpiderService();
						spider.RemoveSpiderLog(projectId, hashcode);
					}
				}
			}
			catch (Exception ex)
			{
				Globals.FileLog.WriteError(ex);
			}
		}

		public static void LoadLog(int projectId)
		{
			lock ((s_ProjectLogs as ICollection).SyncRoot)
			{
				if (!s_ProjectLogs.ContainsKey(projectId))
				{
					com.spider.SpiderService spider = new com.spider.SpiderService();
					DataTable dt = spider.GetSpiderLogsByProjectId(projectId);

					if (dt.Rows.Count > 0)
					{
						Dictionary<int, int> logs = new Dictionary<int, int>();
						foreach (DataRow dr in dt.Rows)
						{
							logs.Add(Convert.ToInt32(dr["HashCode"]), 1);
						}
						s_ProjectLogs.Add(projectId, logs);
					}
				}
			}
		}

		public static bool Exists(int projectId, int hashCode)
		{
			LoadLog(projectId);

			Dictionary<int, int> logs;

			lock ((s_ProjectLogs as ICollection).SyncRoot)
			{
				s_ProjectLogs.TryGetValue(projectId, out logs);
			}

			if (logs == null)
			{
				return false;
			}

			lock ((logs as ICollection).SyncRoot)
			{
				return logs.ContainsKey(hashCode);
			}
		}
	}
}
